package com.example.mongodb1;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.Map;


@SpringBootTest
public class transaction2 {


    @Autowired
    private MongoTemplate mongoTemplate;


    @Test
    void createCollection(){
        MongoCollection<Document> employee = mongoTemplate.createCollection("employee");
        System.out.println(employee);
    }

    @Test
    void save1() throws JsonProcessingException {
       ObjectMapper obj = new ObjectMapper();
       String str = "{\"name\":\"Alice\",\"age\":25}";

//        JsonNode jsonNode = obj.readTree(str);
//        jsonNode.c
        Map<String,Object> map = obj.readValue(str, Map.class);
        Map<String, Object> employee = mongoTemplate.save(map, "employee");
        System.out.println(employee);

    }
}
